﻿using System;
using System.Web.Security;
using EzDesk.Core.Model;
using EzDesk.Core.ServiceImpl.Repositories;
using EzDesk.Core.ServiceImpl.Services;
using Moq;
using NUnit.Framework;
using EzDesk.Core.Interface;

namespace EzDesk.Core.UnitTest
{
    [TestFixture]
    public class CoreClientLoginServiceFixture
    {
        private ICoreClientLoginService _loginService;

        private Mock<ICoreService> mock_ICoreService;

        private Mock<MembershipProvider> mock_MembershipProvider;
        private Mock<IMembershipService> mock_IMembershipService;


        [SetUp]
        public void SetUp()
        {
            this.mock_ICoreService = new Mock<ICoreService>();
            this.mock_MembershipProvider = new Mock<MembershipProvider>();
            this.mock_IMembershipService = new Mock<IMembershipService>();

            _loginService = new CoreClientLoginService(
                mock_ICoreService.Object,
                mock_IMembershipService.Object
                );
        }

        [Test]
        public void Test_Login_Success()
        {
            string userName = "Superwolf";
            string password = "runaway";
            UserProfile userProfile = new UserProfile();

            mock_MembershipProvider.Setup(mb => mb.ValidateUser(It.IsRegex(userName), It.IsRegex(password))).Returns(true);
            mock_IMembershipService.Setup(s => s.Provider).Returns(mock_MembershipProvider.Object);
            mock_ICoreService.Setup(s => s.GetUserProfileByUserName(It.IsRegex(userName))).Returns(userProfile);

            UserProfile actualUserProfile = _loginService.Login(userName, password);

            mock_MembershipProvider.VerifyAll();
            mock_ICoreService.VerifyAll();
            mock_IMembershipService.VerifyAll();

            Assert.AreSame(userProfile, actualUserProfile);
        }


        [Test]
        public void Test_根据用户名返回系统中的完整用户()
        {
            EzDeskDataContext db = new EzDeskDataContext();
            CoreService _realCoreService = new CoreService(
                new UserRepository(db), 
                new DeptRepository(db), 
                new AppRepository(db), 
                new PositionRepository(db), 
                new GroupRepository(db), 
                new FeedRepository(db),
                new PermissionRepository(db), 
                new OrganizationRepository(db)
                );

            _loginService = new CoreClientLoginService(_realCoreService, mock_IMembershipService.Object);

            Assert.AreEqual(_loginService.GetUserNameForLogin("yux")[0].Name, "杨裕欣");
            Assert.AreEqual(_loginService.GetUserNameForLogin("gw")[0].Name, "龚伟");
            Assert.AreEqual(_loginService.GetUserNameForLogin("xh")[0].Name, "贺旭华");
            Assert.AreEqual(_loginService.GetUserNameForLogin("裕欣")[0].Name, "杨裕欣");
            Assert.AreEqual(_loginService.GetUserNameForLogin("x").Count, 5);

        }
    }
}
